/*
 * © 2010 Autodata Solutions Company. All Rights Reserved
 * This source code is the confidential and proprietary information of Autodata Solutions Company.
 * The user shall not, in whole or in part, copy, publish, disclose or make any use of this source code
 * without the prior written consent of Autodata Solutions Company.
 */
Ext.ns('net.autodata.compare');
(function () {
	
	/**
	 * This view class is responsible for controlling how the common competitor's list is rendered
	 * @class CommonCompetitorsView
	 * @extends net.autodata.AbstractView
	 * @namespace net.autodata.compare
	 */
	var View = Ext.extend(net.autodata.AbstractView, {
	
		constructor: function (cfg)
		{
			cfg = cfg || {};
			this.initialConfig = cfg;
		},
		
		/**
		 * @method init
		 */
		init: function ()
		{
			View.superclass.init.apply(this, arguments);
			
			var store = this.component.getStore();
			
			store.on({
				'load': this.refresh,
				scope: this
			});
		},
		
		/**
		 * @method initTemplates
		 */
		initTemplates: function ()
		{
			var ts = this.templates || {};
			
			this.templates = ts;
			
			if (!ts.competitorTpl) {
				ts.competitorTpl = new Ext.XTemplate(
					'<li acode="{acode}">{modelYear} {make} {model} {trim}</li>',
					{
						compliled: true,
						disableFormats: true
					}
				);
			}
			
			if (!ts.mainTpl) {
				ts.mainTpl = new Ext.Template(
					'<div class="header"><h5>' + ASC.message("net.autodata.compare.vehiclepanel.label.common-competitors") + '</h5></div>',
					'<div class="body">',
						'<ul>',
							'{items}',
						'</ul>',
					'</div>',
					{
						compiled: true,
						disableFormats: true
					}
				);
			}
						
			View.superclass.initTemplates.apply(this, arguments);
		},
		
		/**
		 * Update the rendered list of competitors
		 * @method refresh
		 */
		refresh: function ()
		{
			var c = this.component;
			var ts = this.templates;
			
			var competitors = [];
			
			Ext.each(c.getStore().getRange(), function (record, index) {
				competitors.push(ts.competitorTpl.apply(record.data));
			}, this);
			
			ts.mainTpl.overwrite(c.el.dom, {items: competitors.join('')});
		}	
	});

	net.autodata.compare.CommonCompetitorsView = View;
})();